#include <bitset>
#include <map>
#include <string.h>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
#include "evpath.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>

#define MAX_NUM_SLAVE 1000
#define MAX_LINE_LENGTH 100
#define NUM_COLLECT 300
#define LBW_LENGTH 100
#define NUM_SERVICES 8

typedef struct _simple_rec {
    int event_type; // -1 means initialization state; 0 means data collection state
    char cm_id[2048]; // the contact id of each node;
    int integer_field;
    int data_id;
    char host_name[50];
    long collect_time;
    float cpu;
    float mem;
    float req;
    float entropy;
    float cpuAVE;
    float reqAVE;
    int entropy_ready;
    long flume_latency;
    char m_data[5000000];
    char debug[40960];
    int cArray_size;
    double* cArray;
    int num_connection;
} simple_rec, *simple_rec_ptr;

static FMField simple_field_list[] =
{
    {"event_type", "integer", sizeof(int), FMOffset(simple_rec_ptr, event_type)},
    {"cm_id", "char[2048]", sizeof(char), FMOffset(simple_rec_ptr, cm_id)},
    {"integer_field", "integer", sizeof(int), FMOffset(simple_rec_ptr, integer_field)},
    {"data_id", "integer", sizeof(int), FMOffset(simple_rec_ptr, data_id)},
    {"host_name", "char[50]", sizeof(char), FMOffset(simple_rec_ptr, host_name)},
    {"collect_time", "integer", sizeof(long), FMOffset(simple_rec_ptr, collect_time)},
    {"cpu", "float", sizeof(float), FMOffset(simple_rec_ptr, cpu)},
    {"mem", "float", sizeof(float), FMOffset(simple_rec_ptr, mem)},
    {"req", "float", sizeof(float), FMOffset(simple_rec_ptr, req)},
    {"entropy", "float", sizeof(float), FMOffset(simple_rec_ptr, entropy)},
    {"cpuAVE", "float", sizeof(float), FMOffset(simple_rec_ptr, cpuAVE)},
    {"reqAVE", "float", sizeof(float), FMOffset(simple_rec_ptr, reqAVE)},
    {"entropy_ready", "integer", sizeof(int), FMOffset(simple_rec_ptr, entropy_ready)},
    {"flume_latency", "integer", sizeof(long), FMOffset(simple_rec_ptr, flume_latency)},
    {"m_data", "char[250000]", sizeof(char), FMOffset(simple_rec_ptr, m_data)},
    {"debug", "char[2048]", sizeof(char), FMOffset(simple_rec_ptr, debug)},
    {"cArray_size", "integer", sizeof(int), FMOffset(simple_rec_ptr, cArray_size)},
    {"cArray", "integer[cArray_size]", sizeof(double), FMOffset(simple_rec_ptr, cArray)}, 
    {"num_connection", "integer", sizeof(int), FMOffset(simple_rec_ptr, num_connection)},
    {NULL, NULL, 0, 0}
};

static FMStructDescRec simple_format_list[] =
{
    {"simple_rec", simple_field_list, sizeof(simple_rec), NULL},
    {NULL, NULL}
};

string Versa_exec(string cmd);
float collectCPU(int period);
float collectMEM(int period);
float collectREQ(int period);
int collectCPUMEM(int period, float* cpu, float* mem);
int max (int *array);
float localEntropy(queue<simple_rec> LBW, int BIN_NUMBER, int RANGE);
long getFlumeLatency(string s);
int get_index(char* ids[], char* id, int num_ids);
string get_flume_debug(char* host_name, int restarted);
string get_hbase_debug(char* host_name); 
int collectConnection(double []);
int simple_handler(CManager cm, void *vevent, void *client_data, attr_list attrs);
